package com.smartleanx.module.hrm.service.contracttype;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.common.enums.BizTypeEnum;
import com.smartleanx.module.hrm.eums.file.BizFileTypeEnum;
import com.smartleanx.module.hrm.intergration.FileServiceProvider;
import com.smartleanx.module.infra.api.file.dto.FileRefQueryDTO;
import com.smartleanx.module.infra.api.file.dto.FileRefRespDTO;
import com.smartleanx.module.infra.api.file.dto.FileRefSaveBaseDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.smartleanx.framework.common.util.collection.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.smartleanx.module.hrm.controller.admin.contracttype.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.contracttype.ContractTypeDO;

import com.smartleanx.module.hrm.dal.mysql.contracttype.ContractTypeMapper;


/**
 * 标准合同库分类 Service 实现类
 *
 * @author 精益大师
 */
@Service
@Validated
public class ContractTypeServiceImpl implements ContractTypeService {

    @Resource
    private ContractTypeMapper contractTypeMapper;
    @Resource
    private FileServiceProvider fileServiceProvider;

    /**
     * 构建标准合同库分类树形结构
     */
    public List<ContractTypeTreeListRespVo> getContractTypeTreeList() {
        // 获取合同分类库关联文件
        FileRefQueryDTO refQueryDTO = new FileRefQueryDTO();
        refQueryDTO.setBizType(BizTypeEnum.COMPANY_DOCUMENTS.getValue());
        refQueryDTO.setIgnoreTenant(true);
        List<FileRefRespDTO> correlations = fileServiceProvider.getFileRef(refQueryDTO);
        // 获取所有
        List<ContractTypeDO> contractTypeDOS = contractTypeMapper.selectList();

        // 先根据分类id，给各节点赋值文件数量
        List<ContractTypeTreeListRespVo> respVoList = BeanUtil.copyToList(contractTypeDOS, ContractTypeTreeListRespVo.class)
        .stream().sorted(Comparator.comparing(ContractTypeTreeListRespVo::getId).reversed()).collect(Collectors.toList());
        if (correlations.size() > 0) {
            for (ContractTypeTreeListRespVo vo : respVoList) {
                Integer count = correlations.stream().filter(i -> i.getBizRecordId().equals(vo.getId())).collect(Collectors.toList()).size();
                vo.setCount(count);
            }
            Map<Long, List<ContractTypeTreeListRespVo>> listMap = respVoList.stream().collect(Collectors.groupingBy(ContractTypeTreeListRespVo::getParentId));
            respVoList.forEach(i -> {
                List<ContractTypeTreeListRespVo> vos = listMap.get(i.getId());
                if (CollUtil.isNotEmpty(vos)){
                    i.setCount(vos.stream().mapToInt(ContractTypeTreeListRespVo::getCount).sum());
                }

            });
        }
        return respVoList.stream().sorted(Comparator.comparing(ContractTypeTreeListRespVo::getId)).collect(Collectors.toList());
    }

    /**
     * 获取当前文档分类类型下所有文件
     *
     * @param id 当前节点id
     * @return
     */
    public List<FileRefRespDTO> getChildrenFile(Long id, String name) {
        FileRefQueryDTO refQueryDTO = new FileRefQueryDTO();
        refQueryDTO.setBizType(BizTypeEnum.COMPANY_DOCUMENTS.getValue());
        refQueryDTO.setBizRecordIds(Arrays.asList(id));
        refQueryDTO.setName(name);
        refQueryDTO.setIgnoreTenant(true);
        return fileServiceProvider.getFileRef(refQueryDTO);
    }

    /**
     * 新增文档库类型文件关联信息
     *
     * @param vo
     * @return
     */
    public boolean createContractFileRef(ContractFileRefReqVo vo) {
        List<FileRefSaveBaseDTO> fileRefList = CollectionUtils.convertList(vo.getFileIds(), id -> {
            FileRefSaveBaseDTO dto = new FileRefSaveBaseDTO();
            dto.setFileId(id);
            dto.setBizFileType(BizFileTypeEnum.FILE.getValue());
            return dto;
        });
        return fileServiceProvider.createFileRefByBizTypeAndRecordId(fileRefList, BizTypeEnum.COMPANY_DOCUMENTS.getValue(), vo.getContractTypeId());
    }


}
